package gggameserver.testbed;

import java.io.IOException;

import org.apache.log4j.Logger;

import gggameserver.exceptions.GGTimeout;
import gggameserver.games.connection.GGConnection;

public class GGRemoteConnection implements GGConnection {

    private static org.apache.log4j.Logger log = Logger.getLogger(GGRemoteConnection.class);
	
    public static final String CMD_RC_CLOSE = "rc-close";
    public static final String CMD_RC_RECEIVE = "rc-receiveCommand";
    public static final String CMD_RC_SEND = "rc-sendCommand";
    
    private GGRemoteTestBed testbed;

    public GGRemoteConnection(GGRemoteTestBed tb) {
    	this.testbed = tb;
    }
    
    @Override
	public void close() {
    	try {
			testbed.sendCommand(CMD_RC_CLOSE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.warn("aaa", e);
		}
	}

	@Override
	public String receiveCommand(long timeout) throws GGTimeout {
		try {
			byte[][] data = 
				testbed.sendCommand(CMD_RC_RECEIVE, (int)timeout); // TODO int->long
			return testbed.ba2string(data[1]);
		} catch (IOException e) {
			// TODO sendCommand IOException!!!
			e.printStackTrace();
			return null;
		} 
	}

	@Override
	public void sendCommand(String cmd) {
		try {
			byte[][] data = 
				testbed.sendCommand(CMD_RC_SEND, cmd);
		} catch (IOException e) {
			// TODO sendCommand IOException!!!
			e.printStackTrace();
		}
	}

}
